package com.wuwangfu.stream;

import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

/**
 * @Author jcshen
 * @Date 2023-02-21
 * @PackageName:com.wuwangfu.stream
 * @ClassName: DKafakSource
 * @Description: 旧版本API
 * @Version 1.0
 *
 *  https://2fwww.dandelioncloud.cn/article/details/1568438131680833538
 *  wget https://archive.apache.org/dist/kafka/2.4.1/kafka_2.11-2.4.1.tgz
 *
 */
public class DKafakSource {
    public static void main(String[] args) throws Exception {
        Configuration config = new Configuration();
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(config);

        Properties props = new Properties();
        //Kafka地址和端口
        props.setProperty("bootstrap.servers","node01:9092");
        //读取偏移量策略：如果没有记录偏移量，就从头读取，如果记录偏移量，就接着读取
        props.setProperty("auto.offset.reset","earliest");
        //设置消费者组ID
        props.setProperty("group.id","ktv");
        //没有开启checkpoint，让flink提交偏移量的消费者定期自动提交偏移量
        props.setProperty("enable.auto.commit","true");

        FlinkKafkaConsumer<String> kafkaConsumer = new FlinkKafkaConsumer<>("ktv",//Kafka数据top
                new SimpleStringSchema(), //读取文件反序列化Schema
                props//Kafka相关参数
        );

        //接入第三方或自定义数据源
        DataStreamSource<String> line = env.addSource(kafkaConsumer);

        System.out.println(line.getParallelism());
        line.print();

        env.execute();
    }
}
